//
//  ExportPartController.m
//  RedballBar
//
//  Created by Faith on 2024/9/3.
//

#import "ContestPartPageController.h"
#import "ContestPartPageCell.h"

@interface ContestPartPageController () <UICollectionViewDelegate, UICollectionViewDataSource>

/// 专家数据控件
@property (nonatomic, strong) UICollectionView *contentView;

/// 专家数据
@property (nonatomic, strong) NSArray *dataList;

@property (nonatomic, strong) NSDictionary *cacheList;

@end

@implementation ContestPartPageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.contentView];
    self.contentView.layer.cornerRadius = 5;
    self.contentView.layer.masksToBounds = YES;
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
}

- (UICollectionView *)contentView {
    if(!_contentView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
        layout.minimumInteritemSpacing = SIZE(10); // 每列之间的最小间距
        layout.minimumLineSpacing = SIZE(10); // 每行之间的最小间距
        layout.sectionInset = UIEdgeInsetsMake(SIZE(10), SIZE(10), SIZE(10), SIZE(10)); // 上下左右间距
        _contentView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _contentView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _contentView.delegate = self;
        _contentView.dataSource = self;
        [_contentView registerClass:[ContestPartPageCell class] forCellWithReuseIdentifier:@"kContestPartPageCell"];
        [self.contentView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
    }
    return _contentView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ContestPartPageCell *contestPartPageCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kContestPartPageCell" forIndexPath:indexPath];
    [contestPartPageCell setData:self.dataList[indexPath.row]];
    return contestPartPageCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat collectionViewWidth = CGRectGetWidth(collectionView.frame);
    CGFloat sectionInsetLeft = ((UICollectionViewFlowLayout *)collectionViewLayout).sectionInset.left;
    CGFloat sectionInsetRight = ((UICollectionViewFlowLayout *)collectionViewLayout).sectionInset.right;
    CGFloat minimumInteritemSpacing = ((UICollectionViewFlowLayout *)collectionViewLayout).minimumInteritemSpacing;
    CGFloat totalSpacing = sectionInsetLeft + sectionInsetRight + (1 * minimumInteritemSpacing);
    CGFloat itemWidth = (collectionViewWidth - totalSpacing) / 2;
    return CGSizeMake(itemWidth, itemWidth); // 假设 item 的高度等于宽度
}

/// 设置数据
- (void)setData:(NSArray*)dataList
{
    self.dataList = [[NSMutableArray alloc] initWithArray:dataList];
    [self.contentView reloadData];
}

/**
 * 监听属性变化
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"contentSize"]) {
        CGFloat height = self.contentView.contentSize.height;
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(height);
        }];
    }
}

@end
